package com.xlong.mysql.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class SystemDao {
	@Autowired
    private JdbcTemplate jdbcTemplate;
	
	
	/**
     * 返回一个账号所拥有的权限码集合 
     */
	public List<String> getRightsByUserName(String username){
		String sql = "SELECT rig.rname FROM tbl_user u LEFT JOIN tbl_user_role ur on ur.userid = u.id LEFT JOIN tbl_role_right rr on rr.roleid = ur.roleid LEFT JOIN tbl_rights rig on rig.id = rr.rightid where u.username = ?";
        Object[] params = {username};
        try {
        	return jdbcTemplate.queryForList(sql, String.class,params);
        }catch(Exception ex) {
        	return new ArrayList<String>();
        }
	}
	
	/**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
	public List<String> getRolelistByUserName(String username){
		String sql = "select role.rolename from tbl_user u left join tbl_user_role ur on ur.userid = u.id LEFT JOIN tbl_role role on role.id = ur.roleid where u.username = ?";
        Object[] params = {username};
        try {
        	return jdbcTemplate.queryForList(sql, String.class,params);
        }catch(Exception ex) {
        	return new ArrayList<String>();
        }
	}
	

}
